<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Controller;
use think\Request;

class Order extends Backend
{

    public function __construct()
    {
        parent::__construct();
        // $this->ProjectModel = model('Project.Project');
        // $this->ProjectOrderModel = model('Project.ProjectOrder');
        $this->OrderModel = model('Order.Order');
        $this->OrderProductModel = model('Order.OrderProduct');
        $this->ExpressModel = model('Express');
        $this->ProductModel = model('Product.Product');
    }

    // 订单列表
    public function order()
    {

        $OrderList = $this->OrderModel->with(['express', 'user', 'address'])
            ->order('createtime')->paginate(5, false, ['query' => Request::instance()->param()]);

        // 条件搜索
        if ($this->request->isGet()) {
            $action = $this->request->param('action');
            if ($action == 'search') {
                $params = $this->request->param();
                if ($params['search'] == 'nickname') {
                    $OrderList = $this->OrderModel->with('user')->where('user.nickname', 'LIKE', '%' . $params['title'] . '%')->paginate(
                        4,
                        false,
                        ['query' => Request::instance()->param()]
                    );
                }
            }
            if ($action == 'search') {
                $params = $this->request->param();
                if ($params['search'] == 'code') {
                    $OrderList = $this->OrderModel->where('code', 'LIKE', '%' . $params['title'] . '%')->paginate(
                        4,
                        false,
                        ['query' => Request::instance()->param()]
                    );
                }
            }
        }

        $navTitle = '订单管理';
        $navSmall = '订单列表';
        return $this->fetch('', [
            'OrderList' => $OrderList,
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
        ]);
    }

    // 发货
    public function deliver()
    {
        if ($this->request->isAjax()) {
            // 查询物流公司
            $action = $this->request->param('action');
            global $orderid;
            // halt($orderid);
            if ($action == 'deliver') {
                $express = $this->ExpressModel->select();
                if ($express == null) {
                    $this->error('暂无配送公司');
                }
                $this->success('配送公司查询成功', null, $express);
            }
            // 确定发货
            if ($action == 'ok') {
                $expid = $this->request->param('id');
                $orderid = $this->request->param('orderid');

                $express = $this->ExpressModel->find($expid);
                if (!$express) {
                    $this->error('该物流公司不存在');
                }

                $data = [
                    'id' => $orderid,
                    'expid' => $expid,
                    'status' => 2
                ];
                $data['expcode'] = build_code($express['prefix']);
                // halt($data);
                $res = $this->OrderModel->isUpdate(true)->save($data);
                if ($res == false) {
                    $this->error('发货失败');
                }
                $this->success('发货成功');
            }
        }
    }

    // 订单详情
    public function info()
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action == 'info') {
                $id = $this->request->param('id');
                $OrderInfo = $this->OrderModel->with(['express', 'user', 'address'])->where('order.id', $id)->find();
                $pro = $this->OrderProductModel->where('orderid', $OrderInfo['id'])->select();
                $numArr = [];
                $proArr = [];
                foreach ($pro as $item) {
                    // $num = bcadd($num, $item['nums']);
                    $name = $this->ProductModel->where('id', $item['proid'])->column('name, price');
                    array_push($proArr, $name);
                    $name1 = $this->ProductModel->where('id', $item['proid'])->value('name');
                    $a = [
                        'name' => $name1,
                        'num' => $item['nums']
                    ];
                    array_push($numArr, $a);
                }
                $OrderInfo['numArr'] = $numArr;
                $OrderInfo['proArr'] = $proArr;
                if (!$OrderInfo) {
                    $this->error('查询订单数据失败');
                }
                $this->success('查询订单数据成功', null, $OrderInfo);
            }
        }
    }

    // 物流公司列表
    public function company()
    {
        $express = $this->ExpressModel->paginate(5, false, ['query' => Request::instance()->param()]);

        // 条件搜索
        if ($this->request->isGet()) {
            $action = $this->request->param('action');
            if ($action == 'search') {
                $params = $this->request->param();
                if ($params['search'] == 'expname') {
                    $express = $this->ExpressModel->where('name', 'LIKE', '%' . $params['name'] . '%')
                        ->paginate(
                            4,
                            false,
                            ['query' => Request::instance()->param()]
                        );
                }
            }
        }

        // 添加
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action == 'InputChange') {
                $value = $this->request->param('value');
                $data = $this->ExpressModel->where('name', $value)->find();

                if ($data != null) {
                    $this->error('公司名称已存在', null, $data);
                }
                $this->success('公司名称可以使用', null, $data);
            }
            if ($action == 'InputChange1') {
                $prefix = $this->request->param('value');
                $prefix = trim($prefix);
                $data1 = $this->ExpressModel->where('prefix', $prefix)->find();
                if ($data1 != null) {
                    $this->error('订单前缀已存在', null, $data1);
                }
                $this->success('订单前缀可以使用', null, $data1);
            }
            if ($action == 'addCate') {
                $expName = $this->request->param('expName');
                $prefix = $this->request->param('prefix');
                $expName = trim($expName);
                $prefix = trim($prefix);
                $dataN = $this->ExpressModel->where('name', $expName)->find();
                $dataP = $this->ExpressModel->where('prefix', $prefix)->find();
                if ($dataN != null) {
                    $this->error('公司名称已存在');
                }
                if ($dataP != null) {
                    $this->error('订单前缀已存在');
                }
                $CateDate = [
                    'name' => $expName,
                    'prefix' => $prefix
                ];
                $res = $this->ExpressModel->save($CateDate);
                if ($res == false) {
                    $this->error('添加失败');
                }
                $this->success('添加成功', url('admin/order/company'));
            }
        }

        $navTitle = '订单管理';
        $navSmall = '物流公司列表';
        return $this->fetch('', [
            'express' => $express,
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
        ]);
    }

    // 编辑配送公司
    public function expEdit()
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            // 点击获取数据
            if ($action == 'editCate') {
                $id = $this->request->param('id');
                $data = $this->ExpressModel->find($id);
                if ($data == null) {
                    $this->error('未知错误,无法修改信息');
                }
                $this->success('允许修改公司信息', null, $data);
            }
            // 确定修改
            if ($action == 'SaveEdit') {
                $saveid = $this->request->param('id');
                $prefix1 = $this->request->param('prefix1');
                $prefix1tate = $this->ExpressModel->where('name', $prefix1)->find();
                if ($prefix1tate != null) {
                    $this->error('订单前缀已存在');
                }
                $CateDate = [
                    'id' => $saveid,
                    'prefix' => $prefix1,
                ];
                // halt($CateDate);
                $res = $this->ExpressModel->isUpdate(true)->save($CateDate);
                if ($res == false) {
                    $this->error('更新失败');
                }
                $this->success('更新成功', url('admin/cate/index'));
            }
        }
    }

    // 删除配送公司
    public function del()
    {
        $id = $this->request->param('id');
        $CateState  = $this->ExpressModel->find($id);
        if ($CateState == null) {
            $this->error('该分类无法删除');
        }
        $res = $this->ExpressModel->where('id', $id)->delete();
        if ($res == false) {
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }

    // 退货列表
    public function return()
    {
        $OrderList = $this->OrderModel->with('user')->where('status', '<', 0)
            ->order('createtime')->paginate(5, false, ['query' => Request::instance()->param()]);

        // 条件搜索
        if ($this->request->isGet()) {
            $action = $this->request->param('action');
            if ($action == 'search') {
                $params = $this->request->param();
                if ($params['search'] == 'code') {
                    $OrderList = $this->OrderModel->where('code', 'LIKE', '%' . $params['code_text'] . '%')
                    ->where('status', '<', 0)->paginate(4,false,['query' => Request::instance()->param()]);
                }
            }
        }

        if ($this->request->isAjax()) {
            if ($this->request->isPost()) {
                $action = $this->request->param('action');
                // 同意退货
                if ($action == 'return1') {
                    $id = $this->request->param('id');
                    $Order = $this->OrderModel->find($id);
                    if (!$Order) {
                        $this->error('订单不存在');
                    }
                    // 新增用户消费表数据
                    $OrderData = [
                        "id" => $id,
                        "status" => -1,
                    ];
                    $OrderState = $this->OrderModel->isUpdate(true)->save($OrderData);
                    if ($OrderState == false) {
                        // $this->UserModel->rollback();
                        $this->error('操作失败');
                    }
                    $this->success('已同意退货', null, null);
                }
                // 拒绝退货
                if ($action == 'return2') {
                    $id = $this->request->param('id');
                    $Order = $this->OrderModel->find($id);
                    if (!$Order) {
                        $this->error('订单不存在');
                    }
                    // 新增用户消费表数据
                    $OrderData = [
                        "id" => $id,
                        "status" => -3,
                    ];
                    $OrderState = $this->OrderModel->isUpdate(true)->save($OrderData);
                    if ($OrderState == false) {
                        // $this->UserModel->rollback();
                        $this->error('操作失败');
                    }
                    $this->success('已不同意退货', null, null);
                }
            }
        }

        $navTitle = '订单管理';
        $navSmall = '退货列表';
        return $this->fetch('', [
            'OrderList' => $OrderList,
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
        ]);
    }
}
